import pandas as pd

meat_to_animal = {
    "bacon": "pig",
    "pastrami": "cow",
    "corned beef": "pig",
    "nova box": "salmon"
}


def update_map(frame):
    # map添加映射或者函数的引用
    # 方式一：参数为字典
    frame["food"].map(meat_to_animal)
    # 方式二：函数的引用
    frame["animal"] = frame["food"].map(lambda x: meat_to_animal.get(x))
    print(frame)


def update_applymap():
    data = {
        "age": [18, 20, 22],
        "height": [165, 175, 185]
    }
    frame = pd.DataFrame(data)
    print(frame.applymap(lambda x: x * x if x % 2 == 0 else x / 2))


def update_apply():
    data = {
        "age": [18, 20, 22],
        "height": [165, 175, 185]
    }
    frame = pd.DataFrame(data)
    print(frame.apply(lambda x: x.max()))


if __name__ == '__main__':
    data = {
        "food": ["bacon", "pulled pork", "pastrami", "corned beef", "bacon", "pastrami", "honey ham", "nova lox"],
        "ounces": [4, 3, 12, 6, 7.5, 8, 3, 5]
    }
    frame = pd.DataFrame(data)
    # update_map(frame)
    # update_applymap()
    update_apply()
